package com.good.child.ui.activity.ai;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.good.child.MainActivity;
import com.good.child.R;
import com.good.child.http.CommunityLogical;
import com.good.child.ui.adapter.ai.NineGridAdapter;
import com.good.child.ui.adapter.ai.OnAddPicturesListener;
import com.sjy.pickphotos.pickphotos.PhotoPicker;
import com.sjy.pickphotos.pickphotos.listeners.OnResultListener;
import com.yimidida.library.base.BaseActivity;
import com.yimidida.library.base.BasePresenter;
import com.yimidida.library.base.ICallBackResultListener;
import com.yimidida.library.tool.TActivityUtils;
import com.yimidida.library.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 社区发表
 */
public class SelectImageActivity extends BaseActivity {
    private static final int REQUEST_IMAGE = 2;
    private int maxNum = 4;
    @BindView(R.id.rv_images)
    RecyclerView rvImages;
    @BindView(R.id.et_remark)
    EditText contentEd;
   @BindView(R.id.tv_publish)
   TextView publishTv;

    private NineGridAdapter adapter;

    private List<String> mSelectList;
    private String content = "";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_image);
        ButterKnife.bind(this);
        initView();
    }

    @Override
    protected BasePresenter initPresenter() {
        return null;
    }

    @Override
    protected void initView() {
        setStatusBar(false, true, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        setAndroidNativeLightStatusBar(this, true);
        setTitle("社区发表");
        setViewLineVisable(false);
        mSelectList = new ArrayList<>();
        rvImages.setLayoutManager(new GridLayoutManager(this, 3));
        adapter = new NineGridAdapter(this, mSelectList, rvImages);
        adapter.setMaxSize(maxNum);
        rvImages.setAdapter(adapter);
        adapter.setOnAddPicturesListener(new OnAddPicturesListener() {
            @Override
            public void onAdd() {
                pickImage();
            }
        });
    }

    private void pickImage() {
        PhotoPicker.Album(this)
                .setMultiChooseSize(maxNum)
                .setIsCompress(true)//是否压缩
                .setIsCrop(false)//是否裁切
                .setOnResultListener(new OnResultListener() {
                    @Override
                    public void onSucess(ArrayList<String> imagePathList) {
                        mSelectList.clear();
                        mSelectList.addAll(imagePathList);
                        adapter.notifyDataSetChanged();
                    }

                    @Override
                    public void onCancel() {

                    }
                }).start();
    }

    @OnClick({R.id.tv_publish})
    public void submit() {
        content = contentEd.getText().toString();
        if (TextUtils.isEmpty(content)) {
            ToastUtil.showShort("请输入文字");
            return;
        }
        if (mSelectList.isEmpty()) {
            return;
        }
        CommunityLogical.getInstance().requestConstants(this, mSelectList,content, new ICallBackResultListener() {
            @Override
            public void onCallBack(Object result) {
                if (result != null && (Boolean) result) {//成功
                    ToastUtil.showShort("发布成功");
                    finish();
                }
            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_HOME) {
            TActivityUtils.jumpToActivity(getBaseContext(), MainActivity.class);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }


}